//
//  UserInfoViewController.m
//  JinMaJiaDemo
//
//  Created by GripMeMory on 16/1/20.
//  Copyright © 2016年 GripMeMory. All rights reserved.
//

#import "UserInfoViewController.h"
#import "UserInfoCell.h"

#import "UserChangeViewController.h"

#define kCellHeight 100 / 2

#define kTitleLabelLeftMargin 30 / 2
#define kTitleLabelRightMargin 25 / 2
#define kDetailLabelRightMargin 20 / 2
#define kEnterImageViewWidth 20 / 2
#define kSwitchWidth 80 / 2
#define kSwitchTopMargin 15 / 2

NSString * const cellIdentifier1 = @"userInfoCell";

@interface UserInfoViewController () <UITableViewDataSource, UITableViewDelegate>

@end

@implementation UserInfoViewController

{
    UITableView *_tableView;
    NSMutableArray *_modelList;
    UIView *_enterView;
    UIView *_enterViewSeparateView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeValue];
    [self customNav];
    [self customMainView];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.tabBarController.tabBar.hidden = NO;
}

#pragma mark - Custom User Interface

- (void)makeValue{
    
    _modelList = [NSMutableArray arrayWithCapacity:10];
    
    NSArray *titleArr = @[@"用户名", @"注册人身份", @"联系人姓名", @"手机", @"邮箱", @"修改登录密码"];
//    , @"开启手势", @"修改手势密码"];
    NSArray *detailArr = @[@"金马甲", @"个人", @"金小萌", @"15100398769", @"jmj@jmj.com", @""];
//    , @"", @""];
    
    for (int i = 0; i < 6; i++) {
        UserInfoModel *model = [[UserInfoModel alloc] init];
        model.title = titleArr[i];
        model.detail = detailArr[i];
        if (i < 3) {
            model.type = NO;
        }else{
            model.type = YES;
        }
        [_modelList addObject:model];
    }
}

- (void)customNav{
    self.title = @"账户中心";
    UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回"] style:UIBarButtonItemStylePlain target:self action:@selector(bbiClick:)];
    self.navigationItem.leftBarButtonItem = bbi;
}

- (void)customMainView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UISCREENWIDTH, kCellHeight * 8) style:UITableViewStyleGrouped];
    [_tableView registerClass:[UserInfoCell class] forCellReuseIdentifier:cellIdentifier1];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = RGB_COLOR(@"#f6f6f6");
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.scrollEnabled = NO;
    [self.view addSubview:_tableView];
    
    self.view.backgroundColor = RGB_COLOR(@"#f6f6f6");
    
    UIView *gesturePasswordView = [[UIView alloc] initWithFrame:CGRectMake(0, _tableView.bottom - 1, _tableView.width, kCellHeight)];
    UILabel *gestureTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kTitleLabelLeftMargin, 0, _tableView.width, kCellHeight)];
    gestureTitleLabel.text = @"开启手势";
    gestureTitleLabel.textAlignment = NSTextAlignmentLeft;
    UISwitch *gesturePasswordSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(UISCREENWIDTH - kTitleLabelRightMargin - kSwitchWidth - kDetailLabelRightMargin, kSwitchTopMargin, kSwitchWidth, kCellHeight - 2 *kSwitchTopMargin)];
    gesturePasswordSwitch.selected = YES;
    [gesturePasswordView addSubview:gestureTitleLabel];
    [gesturePasswordView addSubview:gesturePasswordSwitch];
    gesturePasswordView.backgroundColor = [UIColor whiteColor];
    [gesturePasswordSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:gesturePasswordView];
    
    _enterView = [[UIView alloc] initWithFrame:CGRectMake(0, gesturePasswordView.bottom, gesturePasswordView.width, kCellHeight)];
    _enterView.backgroundColor = [UIColor whiteColor];
    UILabel *enterTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kTitleLabelLeftMargin, 0, (UISCREENWIDTH - kTitleLabelLeftMargin - kTitleLabelRightMargin - kDetailLabelRightMargin - kEnterImageViewWidth) / 2, kCellHeight)];
    enterTitleLabel.text = @"修改手势密码";
    UIImageView *enterImageView = [[UIImageView alloc] initWithFrame:CGRectMake(_enterView.right - kTitleLabelRightMargin - kEnterImageViewWidth, 0, kEnterImageViewWidth, kCellHeight)];
    enterImageView.image = [UIImage imageNamed:@"进入"];
    enterImageView.contentMode = UIViewContentModeScaleAspectFit;
    [_enterView addSubview:enterTitleLabel];
    [_enterView addSubview:enterImageView];
    
    _enterViewSeparateView = [[UIView alloc] initWithFrame:CGRectMake(kTitleLabelLeftMargin, 0, _enterView.width - kTitleLabelLeftMargin - kTitleLabelRightMargin, 0.5)];
    _enterViewSeparateView.backgroundColor = RGB_COLOR(@"#c9c9c9");
    [_enterView addSubview:_enterViewSeparateView];
}

#pragma mark - Actio

- (void)bbiClick:(UIBarButtonItem *)bbi{
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)switchChanged:(UISwitch *)sw{
    sw.selected = !sw.selected;
    if (sw.selected == YES) {
        [_enterView removeFromSuperview];
    }else{
        [self.view addSubview:_enterView];
    }
}

#pragma mark - TableView Datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _modelList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UserInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1 forIndexPath:indexPath];
    if (!cell) {
        cell = [[UserInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier1];
    }
    [cell configureCellWithModel:_modelList[indexPath.row]];
    return cell;
}

#pragma mark - TableView Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kCellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row > 2) {
        UserChangeViewController *ucvc = [[UserChangeViewController alloc] init];
        UserInfoModel *model = _modelList[indexPath.row];
        
        ucvc.changeDetailBlock = ^(NSString *newString){
            model.detail = newString;
            [_tableView reloadData];
        };
        
        if ([model.title isEqualToString:@"手机"]) {
            ucvc.title = @"修改手机号码";
        }else if([model.title isEqualToString:@"邮箱"]){
            ucvc.title = @"修改邮箱";
        }else if ([model.title isEqualToString:@"修改登录密码"]){
            ucvc.title = @"修改登录密码";
        }
        ucvc.originDetail = model.detail;
        [self.navigationController pushViewController:ucvc animated:NO];
    }
}

@end
